import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Request, Query } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Response } from 'express';
import { Platform } from './platform.entity';
import { PlatformService } from './platform.service';
import { User } from '../user/user.entity';
import { UserService } from '../user/user.service';

import {
  apibefore
} from '../../router'
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { ValidTokenInterceptor } from 'src/interceptor/validToken';
import { EnumInternetCelebrityGroupTypeEnum } from '../internetCelebrityGroup/internetCelebrityGroup.enum';
import { ValidUniqueLoginInterceptor } from 'src/interceptor/validUniqueLogin';

@Controller(apibefore('/platform_background'))
export class PlatformControllerBackground {
  constructor(private readonly service: PlatformService,
    private readonly userService: UserService
    ) {}

  @Get('/findAll_By_Ic_Group_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async findAll_By_Ic_Group_auth(@Query() params: {ic_group: number}){
    return await this.service.findAll_By_Ic_Group(params.ic_group)
  }

  @Get('/findAll_By_Ic_Group')
  async findAll_By_Ic_Group(@Query() params: {ic_group: number}){
    return await this.service.findAll_By_Ic_Group(params.ic_group)
  }

  @Post('/addplatform_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async addplatform_auth(@Body() data, @Request() req){
    let findWxUser = await this.userService.findOne(req.headers.token)
    if(!findWxUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let wx_user = findWxUser.wx_user
    let err = await this.service.addPlatformBackground_auth(data.platformList, wx_user, undefined, data.ic_group, EnumInternetCelebrityGroupTypeEnum.overseas)
    if(err){
      return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return true
    }
  }
  

  @Post('/addplatform')
  async addplatform(@Body() data, @Request() req){
    let findWxUser = await this.userService.findOne(req.headers.token)
    if(!findWxUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let wx_user = findWxUser.wx_user
    let err = await this.service.addPlatformBackground(data.platformList, wx_user, undefined, data.ic_group, EnumInternetCelebrityGroupTypeEnum.overseas)
    if(err){
      return new HttpException(err, HttpStatus.INTERNAL_SERVER_ERROR)
    }else{
      return true
    }
  }
  
  @Post('/create_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async create_auth(@Body() params: Platform, @Request() req) {
    let findWxUser = await this.userService.findOne(params.id)
    if(!findWxUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let addItem = new Platform()
    addItem.platform = params.platform
    addItem.create_time = new Date()
    addItem.wx_user = findWxUser.wx_user
    return await this.service.create(addItem)
  }


  @Post('/create')
  async create(@Body() params: Platform, @Request() req) {
    let findWxUser = await this.userService.findOne(params.id)
    if(!findWxUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    let addItem = new Platform()
    addItem.platform = params.platform
    addItem.create_time = new Date()
    addItem.wx_user = findWxUser.wx_user
    return await this.service.create(addItem)
  }

  @Post('/update_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async update_auth(@Body() params: Platform) {
    return await this.service.update_background(params.id, params.platform)
  }

  @Post('/update')
  async update(@Body() params: Platform) {
    return await this.service.update_background(params.id, params.platform)
  }

  @Get('/findAll_By_User2')
  async findAll_By_User2(@Query() params) {
    let findWxUser = await this.userService.findOne(params.id)
    if(!findWxUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    if(!params.pageSize){
      return this.service.findAll_By_User(-1,-1, '', findWxUser.wx_user)
    }
    let pageSize: number = parseInt(params.pageSize)
    let pageIndex: number = parseInt(params.pageIndex)
    let skip: number = pageSize * pageIndex
    return this.service.findAll_By_User(skip, pageSize,params.keyword || '', findWxUser.wx_user)
  }

  @Get('/findAll_By_User_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async findAll_By_User_auth(@Request() req){
    let findWxUser = await this.userService.findOne(req.headers.token)
    if(!findWxUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return this.service.findAll_By_Wx_User(findWxUser.wx_user)
  }

  @Get('/findAll_By_User')
  async findAll_By_User(@Request() req){
    let findWxUser = await this.userService.findOne(req.headers.token)
    if(!findWxUser){
      return new HttpException('用户不存在',HttpStatus.INTERNAL_SERVER_ERROR)
    }
    return this.service.findAll_By_Wx_User(findWxUser.wx_user)
  }


  @Post('/delete_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async delete_auth(@Body() params: { id: number }){
    return await this.service.remove(params.id)
  }

  @Post('/delete')
  async delete(@Body() params: { id: number }){
    return await this.service.remove(params.id)
  }

}
